package com.twotiger.and.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import app.twotiger.p2p.R;
import app.twotiger.p2p.R.color;

import com.twotiger.and.bean.OperationRecord;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.StringUtils;

public class OperationRecordAdapter extends BaseAdapter {

    private LayoutInflater minflater;
    private ArrayList<OperationRecord> mList;
    private Activity mactivity;

    public OperationRecordAdapter(Activity activity, ArrayList<OperationRecord> list) {
        super();
        minflater = LayoutInflater.from(activity);
        mList = list;
        mactivity = activity;
    }

    @Override
    public int getCount() {
        if(ListUtils.isEmpty(mList)){
            return 1;
        }
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = minflater.inflate(R.layout.operation_record_item, null);
        }
        TextView record_item_type = ViewHolder.get(convertView, R.id.record_item_type);
        TextView record_item_state = ViewHolder.get(convertView, R.id.record_item_state);
        TextView record_item_amount = ViewHolder.get(convertView, R.id.record_item_amount);
        TextView record_item_time = ViewHolder.get(convertView, R.id.record_item_time);
        TextView record_item_remain = ViewHolder.get(convertView, R.id.record_item_remain);

        OperationRecord operation = ListUtils.isEmpty(mList) ? null : mList.get(position);
        if (operation != null) {
            record_item_type.setText(operation.getTypeName());
            if (!StringUtils.isEmpty(operation.getStatusName())) {
                record_item_state.setText("[" + operation.getStatusName() + "]");
            } else {
                record_item_state.setText("");
            }
            switch (operation.getIsIncome()) {
            case 1:
                record_item_amount
                    .setText("+" + ArithUtils.coverMoneyComma(operation.getAmount() + ""));
                record_item_amount.setTextColor(mactivity.getResources().getColor(
                    color.capitalflow_income));
                break;
            case -1:
                record_item_amount
                    .setText("-" + ArithUtils.coverMoneyComma(operation.getAmount() + ""));
                record_item_amount.setTextColor(mactivity.getResources().getColor(
                    color.capitalflow_not_income));
                break;
            case 0:
                record_item_amount
                    .setText("" + ArithUtils.coverMoneyComma(operation.getAmount() + ""));
                break;
            default:
                break;
            }
            record_item_time.setText(DateUtil.formatStringToTime(operation.getCtime()));
            record_item_remain
                .setText(ArithUtils.coverMoneyComma(operation.getAccountAmount() + ""));
        }

        return convertView;
    }

    public void resetData(ArrayList rlist) {
        if (!rlist.isEmpty()) {
            mList.clear();
            mList.addAll(rlist);
            notifyDataSetChanged();
        }

    }

    public void addData(ArrayList alist) {
        if (!alist.isEmpty()) {
            mList.addAll(alist);
            notifyDataSetChanged();
        }
    }

}
